package org.example.travel.controller;

import lombok.RequiredArgsConstructor;
import org.example.travel.common.Result;
import org.example.travel.dto.EmailPasswordDTO;
import org.example.travel.dto.EmailRegisterDTO;
import org.example.travel.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/send-code")
    public Result<?> sendCode(@RequestBody Map<String, String> body) {
        String email = body.get("email");
        userService.sendCode(email);
        return Result.success(null);
    }

    @PostMapping("/register")
    public Result<?> register(@RequestBody EmailRegisterDTO dto) {
        userService.register(dto);
        return Result.success(null);
    }

    @PostMapping("/login-by-password")
    public Result<String> login(@RequestBody EmailPasswordDTO dto) {
        String token = userService.login(dto);
        return Result.success(token);
    }
}
